import cmd, sys, os, string

import drectangular,f,analyse, terminate

class CLI(cmd.Cmd):

    def __init__(self, base, configdicts):
        cmd.Cmd.__init__(self)
        self.prompt = '> '
        self.analyse_conf = configdicts[0]
        self.drect_conf = configdicts[1]

    def do_quit(self, arg):
        terminate.exit([self.analyse_conf, self.drect_conf]) 
        
    def do_frename(self, arg):
        arg=string.split(arg,' ')
        if len(arg)<2:
            self.help_frename()
            return
        f.frename(arg[0],arg[1])        
    
    def do_fcopy(self, arg):
        arg=string.split(arg,' ')
        if len(arg)<2:
            self.help_fcopy()
            return
        f.fcopy(arg[0],arg[1])
        
    
    def do_fremove(self, arg):
        if len(arg)<1:
            self.help_fremove()
            return
        f.fremove(arg)
        
    def do_fimport(self, arg):
        pass
    
    def do_fexport(self, arg):
        pass
        
    def do_flist(self, arg):
        sitelist = f.flist()
        for site in sitelist: print site
        
    
    def do_drect_start(self, arg):
        global rect
        rect = drectangular.drect(self.drect_conf)
        
    def do_drect_write(self, arg):        
        try:
            rect.write_to_file(arg)
        except NameError:
            print '    Firstly start drect'
        
    def do_drect_points(self, arg):
        try:
            data=analyse.double_data(f.get_data_list(arg,'fract.txt'))
            rect.draw_points(data)
        except NameError:
            print '    Firstly start drect'
        except IOError:
            print self.help_drect_points()
    
    def do_drect_max(self, arg):
        try:
            data=f.get_data_list(arg,'max.txt')
            rect.draw_points(data)
        except NameError:
            print '    Firstly start drect'
        except IOError:
            print self.help_drect_points()
    
    def drect_point(self, arg):
        arg=string.split(arg,' ')
        arg[0]=int(arg[0]); arg[1]=int(arg[1])
        data=analyse.double_data([arg])
        rect.draw_points(data)
        
    def do_drect_density(self, arg):
        size_of_gridwind = float(self.analyse_conf['size of a grid window'])
        try:
            data=f.get_data_list(arg,'density.txt')
            rect.density_mesh(data,size_of_gridwind)
        except NameError:
            print '    Firstly start drect'
        except IOError:
            if arg in f.flist():
                print "  try 'sdensity' before 'drect_density'"
            else:
                print self.help_drect_density()        
    
    def do_drect_isolines(self, arg):
        size_of_gridwind = float(self.analyse_conf['size of a grid window'])
        try:
            data=f.get_float_data_list(arg,'isolines.txt')
            rect.draw_lines(data, size_of_gridwind)
        except NameError:
            print '    Firstly start drect'
        except IOError:
            if arg in f.flist():
                print "  try 'sisolines' before 'drect_isolines'"
            else:
                print self.help_drect_density()       
    
    def do_sdensity(self,arg):
        size_of_gridwind = int(self.analyse_conf['size of a grid window'])
        size_of_averwind = int(self.analyse_conf['size of an average window'])
        try:
            data = analyse.double_doubled_data(f.get_data_list(arg,'fract.txt'))
            density = analyse.density_grid(data, size_of_gridwind, \
                                                               size_of_averwind)
            f.save_data_list(density,arg,'density.txt')
        except IOError:
            if len(arg)<1:
                print self.help_sdensity()
            elif not (arg in f.flist()):
                print 'Wrong site name:', arg
            else:
                print 'Error'
        
    def do_sisolines(self,arg):       
        intervals_step = float(self.analyse_conf['step of isolines'])
        intervals_limit = float(self.analyse_conf['limit of isolines'])
        try:
            data = f.get_data_list(arg,'density.txt')        
            coords = analyse.isolines(data, intervals_step, intervals_limit)
            f.save_data_list(coords,arg,'isolines.txt')
        except:
            if len(arg)<1:
                print self.help_sisolines()
            elif not (arg in f.flist()):
                print 'Wrong site name:', arg
            else:
                print 'Error'
    
    def do_smax(self, arg):
        size_of_gridwind = int(self.analyse_conf['size of a grid window'])
        try:
            data = f.get_data_list(arg,'density.txt')
            l_max= analyse.local_max(data, size_of_gridwind)
            f.save_data_list(l_max,arg,'max.txt')
            print '    ', len(l_max), ' maximums of density mesh were found'
        except:
            if len(arg)<1:
                print self.help_sisolines()
            elif not (arg in f.flist()):
                print 'Wrong site name:', arg
            else:
                print 'Error'                


    
    def do_set(self, arg):
        Sections=['analyse','drect']
        arg=string.split(arg,',')
        if len(arg)==1:
            if arg[0]=='analyse':
                print 'analyse:'
                for conf in self.analyse_conf:
                    print '    ',conf,'=', self.analyse_conf[conf]
            elif arg[0]=='drect':
                print 'drect:'
                for conf in self.drect_conf:
                    print '    ',conf,'=', self.drect_conf[conf]                
            else:
                print 'analyse:'
                for conf in self.analyse_conf:
                    print '    ',conf,'=', self.analyse_conf[conf]
                print 'drect:'
                for conf in self.drect_conf:
                    print '    ',conf,'=', self.drect_conf[conf] 
        if len(arg)==2:
            conf=string.split(arg[1],'=')
            conf[0]=string.strip(conf[0])
            if arg[0]=='analyse' and len(conf)>1:
                if conf[0] in self.analyse_conf:
                    print conf[0], 'is set to ', \
                                                        string.strip(conf[1])
                    print 'were:', conf[0], '=', self.analyse_conf[conf[0]]
                    
                    self.analyse_conf[conf[0]]=string.strip(conf[1])
                else:
                    print conf[0], "is not in section 'analyse'"
            elif arg[0]=='analyse' and len(conf)==1:
                if conf[0] in self.analyse_conf:
                    print conf[0], '=', self.analyse_conf[conf[0]]
                else:
                    print conf[0], "is not in section 'analyse'"
            
            elif arg[0]=='drect' and len(conf)>1:
                #self.drect_conf[conf[0]]=conf[1]            
                if conf[0] in self.drect_conf:
                    print conf[0], 'is set to ', \
                                                        string.strip(conf[1])
                    print 'were:', conf[0], '=', self.drect_conf[conf[0]]
                    
                    self.drect_conf[conf[0]]=string.strip(conf[1])
                else:
                    print conf[0], "is not in section 'drect'"
            elif arg[0]=='drect' and len(conf)==1:
                if conf[0] in self.drect_conf:
                    print conf[0], '=', self.drect_conf[conf[0]]
                else:
                    print conf[0], "is not in section 'analyse'"
                
    def help_quit(self):
        print "syntax: quit",
        print "-- terminates the application"
        
            
    def help_frename(self):       
        print "syntax: frename [old name of a site]",
        print "[new name]"
        print '-- renames a site'
    
    def help_fcopy(self):
        print "syntax: fcopy [name of a site to copy]",
        print "[name of a new site]"
        print '-- copies files'
    
    def help_fremove(self):
        print "syntax: fremove [name of a site to remove]"    
        print '-- removes a file'
    
    def help_drect_points(self):
        print "syntax: drect_points [site name]"
        print '-- draw a point of a site on a rectangular monitor '
    
    def help_drect_density(self):
        print "syntax: drect_density [site name]"
        print '-- draw a density mesh of a site on a rectangular monitor '
        print "-- before drawing a mesh start 'sdensity' to calculate matrix"
    
    def help_drect_isolines(self):
        print "syntax: drect_isolines [site name]"
        print '-- draw an isolines on a rectangular monitor '
        print "-- before drawing an isolines start 'sisoline' to", \
                                                            "calculate isolines"
    
    def help_sdensity(self):
        print "syntax: sdensity [site name]"
        print '-- calculate a density of points distribution of a site'

    
    def help_sisolines(self):
        print "syntax: sisolines [site name]"
        print '-- calculate an isolines of a mesh'
        print "-- before calculating an isolines start 'sdensity' to", \
                                                            "calculate matrix"
    
    def help_smax(self):
        print "syntax: smax [site name]"
        print '-- calculate maximums of a mesh'
        print "-- before calculating maximums start 'sdensity' to", \
                                                            "calculate matrix"
    
    # shortcuts
    do_q = do_quit
